<template>
  <div class="page" ref="pageHeight">
    <div class="goods-content">
      <div class="filter-content">
        <!-- <span class="label" @click="synthesize" :class="{ 'active-left': !form.sort.price }">
          {{ $t('goods.comprehensiveSorting') }}
        </span> -->
        <!-- <t-sort class="sort" @on-change="onSort" v-model="sortPrice" labelActiveColor="#FF186B"></t-sort> -->

        <Select v-model="sortType" style="width:200px; margin-right: 10px;">
          <Option v-for="item in sortList" :value="item.value" :key="item.value">{{ item.label }}</Option>
        </Select>

        <Row :gutter="12" style="width:380px;">
          <Col span="11">
          <Input v-model="form.priceStart" :placeholder="$t('goods.minPrice')" />
          </Col>
          <Col span="1" style="text-align: center; display: flex;align-items: center;justify-content: center">-</Col>
          <Col span="11">
          <Input v-model="form.priceEnd" :placeholder="$t('goods.maxPrice')" />
          </Col>
          <!-- <Button v-show="form.priceStart || form.priceEnd" @click="onSearch" long type="primary">{{ $t('goods.search')
            }}</Button> -->
        </Row>

        <Button icon="md-search" @click="onSearch" :disabled="!form.priceStart && !form.priceEnd"></Button>
      </div>
      <!-- <Divider /> -->
      <div class="goods-list">
        <product v-for="(item, index) in goodsList" :currencySymbol="userCurrency" :info="item" :key="item.spuNo"
          @taobaoPoint="taobaoPoint" :index="index" :detailPosition="index + 1"></product>
      </div>

      <div class="loading" v-if="goodsList.length == 0 && loading">
        <t-loading></t-loading>
      </div>
      <t-no-data v-if="goodsList.length == 0 && !loading"></t-no-data>
    </div>
    <div class="more" v-if="!noData">
      <Button v-if="goodsList.length > 0" class="btn" :loading="loading" @click="onLoadMore"
        style="border-color: #FF186B;">{{ $t('goods.viewMore')
        }}</Button>
    </div>
    <!-- 禁搜词黑名单 -->
    <tips-text ref="tipsPopRef"></tips-text>
    <!-- 禁搜词灰名单 -->
    <gray-list ref="grayPopRef"></gray-list>
  </div>
</template>

<script>
import Product from "./components/product";
import { productImageQuery, productKeywordQuery, productUploadImg } from "@/api/product";
import TLoading from "@/components/t-loading";
import TSort from "@/components/t-sort";
import TNoData from "@/components/t-no-data";
import { mapState } from "vuex";
import { homeSeatchText, searchPageText, searchListShow, searchListClick } from "@/common/point/taobao";//淘宝埋点
import tipsText from "./components/tips-text.vue";
import GrayList from "./components/gray-list.vue";
export default {
  name: "taobao-goods",
  components: { TNoData, TSort, TLoading, Product, tipsText, GrayList },
  props: {
    loadData: {
      default: false,
      type: Boolean
    }
  },
  data() {
    return {
      loading: false, // 加载
      goodsList: [], // 商品列表
      totalPage: 1, // 总页数
      sortPrice: '', // 排序
      noData: false, // 是否还有更多数据
      form: {
        channel: 'TAOBAO',
        imageId: '',
        keyword: '', // 关键词，必传
        categoryId: '', // 类目id
        filter: '', // 筛选参数，多个通过英文逗号分隔，枚举参见解决方案介绍 shipInToday,ksCiphertext
        outMemberId: '', // 外部用户id
        pageNo: 1, // pageNo,示例值(1)
        pageSize: 20, // pageSize,示例值(20)
        priceEnd: '', // 批发价结束
        priceStart: '', // 批发价开始
        sort: {
          price: ''
        }// 排序参数，枚举参见解决方案介绍,{"price":"asc"}
      },
      sortType: '1', // 排序类型
      sortList: [
        {
          value: '1',
          label: 'Comprehensive Sorting'
        },
        {
          value: '2',
          label: 'Price Sorting（升序）'
        },
        {
          value: '3',
          label: 'Price Sorting（降序）'
        },
      ],
    }
  },
  computed: {
    ...mapState({
      userCurrency: (state) => {
        return state.user.userCurrency.code;
      },
      config: (state) => state.config.config,
      searchAgreement: (state) => {
        // 搜索协议
        let obj = '';
        if (state.config.config) {
          obj = state.config.config.articleMap.CFG_ARTICLE_SEARCH;
        }
        return obj;
      }
    })
  },
  watch: {
    '$route'() {
      this.onSearchGoods();
    },
    loadData() {
      this.onSearchGoods();
    }
  },
  created() {
    this.onSearchGoods();
  },
  methods: {
    // 淘宝埋点
    taobaoPoint(val1, val2) {
      searchListClick(this.search, this.pageNo, val1, val2)
    },
    onSearchGoods() {
      if (!this.loadData) {
        return false;
      }
      this.goodsList = [];
      this.form = {
        channel: 'TAOBAO',
        imageId: '',
        keyword: '', // 关键词，必传
        categoryId: '', // 类目id
        filter: '', // 筛选参数，多个通过英文逗号分隔，枚举参见解决方案介绍 shipInToday,ksCiphertext
        outMemberId: '', // 外部用户id
        pageNo: 1, // pageNo,示例值(1)
        pageSize: 20, // pageSize,示例值(20)
        priceEnd: '', // 批发价结束
        priceStart: '', // 批发价开始
        sort: {
          price: ''
        }// 排序参数，枚举参见解决方案介绍,{"price":"asc"}
      };
      this.noData = false;
      this.search();
    },
    onDefault() {
      // 重置默认数据
      this.goodsList = [];
      this.form.pageNo = 1;
      this.noData = false;
    },
    onSearch() {
      // 搜索商品
      this.onDefault();
      this.search();
    },
    //综合排序
    synthesize() {
      this.sortPrice = '';
      this.form.sort.price = '';
      this.onDefault();
      this.search();
    },
    onSort(name) {
      // 价格排序
      this.form.sort.price = name;
      this.onDefault();
      this.search();
    },
    search() {
      if (this.$route.query.type == 'image') {
        if (this.$route.query.id) {
          this.form.imageId = this.$route.query.id
        }
        this.getProductImageQuery();
      } else {
        let text = this.$route.query.text || this.$route.params.text;
        if (text) {
          this.form.keyword = text
        }
        this.getProductKeywordQuery();
      }
    },
    onLoadMore() {
      // 加载更多
      if (this.form.imageId) {
        this.getProductImageQuery();
      } else {
        this.getProductKeywordQuery();
      }
    },
    getProductImageQuery() {
      // 搜索图片商品
      if (this.noData == true) {
        return false;
      }
      this.loading = true;
      productImageQuery(this.form).then((res) => {
        let data = res.result.records;
        for (let i = 0; i < data.length; i++) {
          this.goodsList.push(data[i]);
        }

        this.form.pageNo = this.form.pageNo + 1;
        if (this.form.pageNo > res.result.pages) {
          this.noData = true;
        }

        this.totalPage = res.result.pages;
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      })
    },
    getProductKeywordQuery() {
      // 搜索文字商品
      if (this.noData == true) {
        return false;
      }
      this.loading = true;
      productKeywordQuery(this.form).then((res) => {
        // 禁搜词黑名单
        if (res.code == 1010) {
          this.loading = false;
          this.$refs.tipsPopRef.open(res.message);
        } else if (res.code == 200) {
          // 禁搜词灰度名单弹框
          if (res.message) {
            this.$refs.grayPopRef.open(res.message);
          }

          homeSeatchText(this.search);//搜索埋点淘宝
          searchPageText(this.search, this.search, this.pageNo)//搜索埋点淘宝
          const itemTaobao = res.result.records.reduce((acc, item, index) => {
            return acc + `${item.spuNo},${index};`;
          }, '');
          searchListShow(this.search, itemTaobao)

          let data = res.result.records;
          for (let i = 0; i < data.length; i++) {
            this.goodsList.push(data[i]);
          }

          this.form.pageNo = this.form.pageNo + 1;
          if (this.form.pageNo > res.result.pages) {
            this.noData = true;
          }

          this.totalPage = res.result.pages;
          this.loading = false;
        }
      }).catch(() => {
        this.loading = false;
      })
    }
  }
}
</script>

<style scoped lang="less">
/deep/.ivu-btn:focus {
  box-shadow: none;
}

/deep/.ivu-input {
  border: none;
  background: #F7F8FB;
  border-radius: 27px;
  padding: 0 31px;
  color: #74767F;
}

.page {
  height: 100%;
  position: relative;

  .scroll-content {
    height: 100%;
  }

  .tabs-wrap {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 30px;
    border-bottom: 1px solid @t-border-color;

    .tabs {
      display: flex;
      align-items: center;

      .tab-item {
        display: flex;
        align-items: center;
        padding: 0px 16px;
        opacity: 1;
        color: @t-title-color;
        font-size: 16px;
        height: 46px;
        border-bottom: 2px solid rgba(0, 0, 0, 0);
        margin: 0 60px;
        cursor: pointer;

        &:hover {
          color: @primary-color;
        }
      }

      .tab-active {
        color: @primary-color;
        border-bottom: 2px solid @primary-color;
      }
    }

    .agreement {
      display: flex;
      align-items: center;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      font-size: 14px;

      .agreement-text {
        color: @t-title-color;
      }

      .agreement-value {
        cursor: pointer;
        color: @primary-color;
        font-weight: 500;
      }
    }
  }

  .filter-content {
    display: flex;
    align-items: center;
    position: absolute;
    right: 0;
    top: -46px;

    .label {
      font-family: PingFang SC, PingFang SC;
      font-weight: 400;
      font-size: 24px;
      color: #767B93;
      line-height: 27px;
      margin-right: 22px;
      min-width: 186px;
      cursor: pointer;
    }

    .active-left {
      color: rgb(255, 24, 107);
    }

    .sort {
      margin-right: 12px;
    }
  }

  .goods-content {
    margin-top: 40px;

    .search-value {
      display: flex;
      align-items: center;
      margin-bottom: 12px;
      color: @t-text-color;
      font-size: 14px;

      span {
        color: @primary-color;
      }

      .search-image {
        display: block;
        width: 80px;
      }
    }

    .goods-list {
      display: grid;
      grid-template-columns: repeat(5, 1fr);
      gap: 20px;
    }
  }

  .more {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    margin-top: 32px;
    margin-bottom: 32px;

    .btn {
      width: 30%;
    }
  }
}
</style>